// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import java.io.File;
import java.util.function.Predicate;
import zombie.debug.DebugLog;
import zombie.util.PZXmlParserException;
import zombie.util.PZXmlUtil;

public final class PredicatedFileWatcher {
    private final String m_path;
    private final Predicate<String> m_predicate;
    private final zombie.PredicatedFileWatcher.IPredicatedFileWatcherCallback m_callback;

    public PredicatedFileWatcher(Predicate<String> predicate, zombie.PredicatedFileWatcher.IPredicatedFileWatcherCallback callback) {
        this(null, predicate, callback);
    }

    public PredicatedFileWatcher(String path, zombie.PredicatedFileWatcher.IPredicatedFileWatcherCallback callback) {
        this(path, null, callback);
    }

    public <T> PredicatedFileWatcher(String path, Class<T> clazz, zombie.PredicatedFileWatcher.IPredicatedDataPacketFileWatcherCallback<T> callback) {
        this(path, null, new zombie.PredicatedFileWatcher.GenericPredicatedFileWatcherCallback(clazz, callback));
    }

    public PredicatedFileWatcher(String path, Predicate<String> predicate, zombie.PredicatedFileWatcher.IPredicatedFileWatcherCallback callback) {
        this.m_path = this.processPath(path);
        this.m_predicate = predicate != null ? predicate : this::pathsEqual;
        this.m_callback = callback;
    }

    public String getPath() {
        return this.m_path;
    }

    private String processPath(String string) {
        return string != null ? zombie.ZomboidFileSystem.processFilePath(string, File.separatorChar) : null;
    }

    private boolean pathsEqual(String string) {
        return string.equals(this.m_path);
    }

    public void onModified(String entryKey) {
        if (this.m_predicate.test(entryKey)) {
            this.m_callback.call(entryKey);
        }
    }

    public static class GenericPredicatedFileWatcherCallback<T> implements zombie.PredicatedFileWatcher.IPredicatedFileWatcherCallback {
        private final Class<T> m_class;
        private final zombie.PredicatedFileWatcher.IPredicatedDataPacketFileWatcherCallback<T> m_callback;

        public GenericPredicatedFileWatcherCallback(Class<T> clazz, zombie.PredicatedFileWatcher.IPredicatedDataPacketFileWatcherCallback<T> callback) {
            this.m_class = clazz;
            this.m_callback = callback;
        }

        @Override
        public void call(String xmlFile) {
            Object object;
            try {
                object = PZXmlUtil.parse(this.m_class, xmlFile);
            } catch (PZXmlParserException pZXmlParserException) {
                DebugLog.General.error("Exception thrown. " + pZXmlParserException);
                return;
            }

            this.m_callback.call((T)object);
        }
    }

    public interface IPredicatedDataPacketFileWatcherCallback<T> {
        void call(T var1);
    }

    public interface IPredicatedFileWatcherCallback {
        void call(String entryKey);
    }
}
